﻿using System;
using Core.AbstractEntities;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Stack.Entities;
namespace Test
{
    /// <summary>
    /// Test the Memory
    /// </summary>
    [TestClass]
    public class UnitTest_Memory
    {
        /// <summary>
        /// Case 2.01 : Instance Memory is not null
        /// </summary>
        [TestMethod]
        public void TestHPMemory_IsNotNull()
        {
            HpPile hpPile = new HpPile();
            Assert.IsNotNull(hpPile.Memory, "Memory doesn't exist");
        }
        /// <summary>
        /// Case 2.02 : The memory “store” differents type of elements in the Memory
        /// The Memory Manager can store items from the heap. 
        /// </summary>
        [TestMethod]
        public void TestHPMemory_StoreElements()
        {
            HpPile hpPile = new HpPile();
            InitialiseForTesthpile(hpPile);
            HpElement element1 = hpPile.Get(2);
            HpElement element2 = hpPile.Get(1);
            //Assign of a value's name for the value to store in memory
            hpPile.Memory.AddElement(element1, "var1");
            hpPile.Memory.AddElement(element2, "var2");
            //Recover the items from the memory by the index (or position in the stack memory)
            Assert.AreEqual('c', hpPile.Memory.Get(0).Value, "Item requested doesn't exist in Memory");
            Assert.AreEqual("this is a string", hpPile.Memory.Get(1).Value, "Item requested doesn't exist in Memory");
        }
        /// <summary> 
        /// Case 2.03 : The memory "recover" one specific item from the Memory 
        /// </summary>
        [TestMethod]
        public void TestHPMemory_RecoverElements()
        {
            HpPile hpPile = new HpPile();
            InitialiseForTesthpile(hpPile);
            HpElement element1 = hpPile.Get(2);
            HpElement element2 = hpPile.Get(1);
            //Assign of a value's name for the value to store in memory
            hpPile.Memory.AddElement(element1, "var1");
            hpPile.Memory.AddElement(element2, "var2");
            //Recover the items from the memory by the Name
            Assert.AreEqual('c', hpPile.Memory.Get("var1").Value, "Item requested doesn't exist in Memory");
            Assert.AreEqual(Type.GetType("System.Char"), hpPile.Memory.Get("var1").Type, "Item requested doesn't exist in Memory");
            Assert.AreEqual("this is a string", hpPile.Memory.Get("var2").Value, "Item requested doesn't exist in Memory");
            Assert.AreEqual(Type.GetType("System.String"), hpPile.Memory.Get("var2").Type, "Item requested doesn't exist in Memory");
        }
        /// <summary> 
        /// Case 2.04 : The memory "delete" one specific item from the Memory 
        /// </summary>
        [TestMethod]
        public void TestHPMemory_DeleteElements()
        {
            HpPile hpPile = new HpPile();
            InitialiseForTesthpile(hpPile);
            HpElement element1 = hpPile.Get(2);
            HpElement element2 = hpPile.Get(1);
            HpElement element3 = hpPile.Get(3);
            //Assign of a value's name for the value to store in memory
            hpPile.Memory.AddElement(element1, "var1");
            hpPile.Memory.AddElement(element2, "var2");
            hpPile.Memory.AddElement(element3, "var3");
            // Verify if there are 3 elements before the "Delete" operation
            Assert.AreEqual(3, hpPile.Memory.Count, "Deleted operation doesn't work!");
            hpPile.Memory.Delete("var2");
            // Verify if there are only 2 elements after the "Delete" operation
            Assert.AreEqual(2, hpPile.Memory.Count, "Deleted operation doesn't work!");

            // Verify if all the others elements are stored
            Assert.AreEqual('c', hpPile.Memory.Get("var1").Value, "Item requested doesn't exist in Memory");
            Assert.AreEqual(3.141516, hpPile.Memory.Get("var3").Value, "Item requested doesn't exist in Memory");
        }
        /// <summary>
        ///Initialitation of the elements in the pile
        /// </summary>
        /// <param name="hpile">Collection</param>
        private static void InitialiseForTesthpile(HpPile hpile)
        {
            HpElement element1 = new HpElement();
            element1.Value = 1;

            HpElement element2 = new HpElement();
            element2.Value = "this is a string";

            HpElement element3 = new HpElement();
            element3.Value = 'c';

            HpElement element4 = new HpElement();
            element4.Value = 3.141516;

            //Add element integer
            hpile.AddElement(element1);
            //Add element string
            hpile.AddElement(element2);
            ////Add element character
            hpile.AddElement(element3);
            ////Add element double
            hpile.AddElement(element4);
        }
    }
}
